/*
  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.

  Copyright (c) 2009 PRO ACTIVE SAS - www.proactive-group.com
   
  Ref_Icode1.c
  -----------

  This is the reference applications that shows how to work with
  NXP ICODE1 cards.
  
  LTX 25/08/2009 : initial release

*/
#include "products/springprox/springprox.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
  SWORD rc;
  BYTE i = 0;
  BYTE block = 0;
  BOOL fast = FALSE;
  TCHAR s_buffer[64];
  DWORD features;
  unsigned char snr[8];
  unsigned char data[256];
  unsigned short len = 256;
  
  char *device = NULL;
  if (argc > 1)
    device = argv[1];

  printf("SpringCard SDK for K531/SpringProx-CF/CSB Legacy\n");
  printf("\n");
  printf("NXP ICODE1 reference demo\n");
  printf("-------------------------------------\n");
  printf("www.springcard.com\n\n");

  rc = SPROX_GetLibrary(s_buffer, sizeof(s_buffer));
  if (rc != MI_OK)
  {
    printf("Failed to get API version\n");
    goto done;
  }
  printf("API version : %s\n", s_buffer);
  printf("\n");

  /* Open reader */
  /* ----------- */

  rc = SPROX_ReaderOpen(device);
  if (rc != MI_OK)
  {
    printf("Reader not found\n");
    goto done;
  }

  printf("Reader found\n");
  rc = SPROX_ReaderGetDevice(s_buffer, sizeof(s_buffer));
  if (rc == MI_OK)
    printf("Reader found on %s\n", s_buffer);

  rc = SPROX_ReaderGetFirmware(s_buffer, sizeof(s_buffer));
  if (rc == MI_OK)
    printf("Reader firwmare is %s\n", s_buffer);

  rc = SPROX_ReaderGetFeatures(&features);
  if ((rc == MI_OK) && !(features & 0x00000002))
  {
    printf("Warning: this reader is not 15693-enabled\n");
    printf("         all subsequent tests will fail !\n");
  }

  printf("\n---------------------------\n");

  /* Configure reader in ICODE1 standard mode */
  /* --------------------------------------- */
  rc = SPROX_SetConfig(CFG_MODE_ICODE1_STD);
  if (rc != MI_OK)
  {
    printf("Failed to configure the reader for a ICODE1 tag\n");
    goto close;
  }

  /* RF field ON */
  SPROX_ControlRF(TRUE);

  /* Select any ICODE1 tag in the RF field */
  /* ------------------------------------- */

  printf("Put your ICODE1 tag on the reader...\n");

  for (;;)
  {
	rc = SPROX_I1_SelectAny(fast,snr);
    if (rc == MI_OK)
	{
      printf("Found ICODE1 tag in RF field\n");
      break;
	}

	if (i == 5)
	{
	  fast = TRUE;
	} else if (i == 10)
	{
	  fast = FALSE;
	  i=0;
	}
	i++;
  }

  printf("Tag found: UID=");
  for (i = 0; i < 8; i++)
    printf("%02X", snr[i]);
  printf("\n");

/* 
 * Read 16 blocks on the tag
 * ------------------------- 
 */
  len = 256;
  rc = SPROX_I1_Read(0, 16, data, &len);
  if (rc != MI_OK)
  {
    printf("Read 16 blocks [Failed]\n");
    printf("  %s (%d)\n", SPROX_GetErrorMessage(rc), rc);
	goto close;
  } else
  {
    printf("[OK]\n");
    for (block = 0; block < 16; block++)
    {
      printf("%02d : ", block);

      for (i = 0; i < 4; i++)
      {
        printf("%02X ", data[4 * block + i]);
      }

      for (i = 0; i < 4; i++)
      {
        if (data[4 * block + i] >= ' ')
        {
          printf("%c", data[4 * block + i]);
        } else
        {
          printf(".");
        }
      }

      printf("\n");
    }
  }

close:
  /* Close the reader */
  SPROX_ReaderClose();

done:
  /* Display last error */
  if (rc == MI_OK)
  {
    printf("Done\n");
  } else
  {
    printf("%s (%d)\n", SPROX_GetErrorMessage(rc), rc);
  }
  return 0;
}
